#encoding: utf-8
class Micropost < ActiveRecord::Base
  attr_accessible :content
  
  validates :user_id , :presence=>true 
  validates :content , :presence=>{:message=> "Campo não deve ficar em branco"} , :length=>{:maximum=> 140}

  default_scope :order=> 'microposts.created_at DESC'

  belongs_to :user

  def self.from_users_followed_by(user)
    followed_user_ids = "SELECT followed_id FROM relationships
                          WHERE follower_id = :user_id"
    where("user_id IN (#{followed_user_ids}) OR user_id = :user_id", user_id: user )
  end
end
